#include "com_voe_VoEEncryption.h"
#include "jni_api.h"
#include "EncryptionImpl.h"
#include "voe_encryption.h"

static EncryptionImpl _Encryption;

/*
 * Class:     com_voe_VoEEncryption
 * Method:    GetInterface
 * Signature: (J)J
 */
JNIEXPORT jlong JNICALL Java_com_voe_VoEEncryption_GetInterface
	(JNIEnv * env, jclass cls, jlong p_voiceEngine)
{
	return (jlong)webrtc::VoEEncryption::GetInterface((webrtc::VoiceEngine*)p_voiceEngine);
}

/*
 * Class:     com_voe_VoEEncryption
 * Method:    Release
 * Signature: (J)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEEncryption_Release
	(JNIEnv * env, jclass cls, jlong p)
{
	webrtc::VoEEncryption* pVoEEncryption = (webrtc::VoEEncryption*)p;
	return pVoEEncryption->Release();
}

/*
 * Class:     com_voe_VoEEncryption
 * Method:    RegisterExternalEncryption
 * Signature: (JILcom/voe/callback/Encryption;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEEncryption_RegisterExternalEncryption
  (JNIEnv * env, jclass cls, jlong p, jint channel, jobject obj)
{
	webrtc::VoEEncryption* pVoEEncryption = (webrtc::VoEEncryption*)p;
	if(_Encryption.set(env, obj))
	{
		return pVoEEncryption->RegisterExternalEncryption(channel, _Encryption);
	}
	return JCB_Err_Set;
}

/*
 * Class:     com_voe_VoEEncryption
 * Method:    DeRegisterExternalEncryption
 * Signature: (JI)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEEncryption_DeRegisterExternalEncryption
	(JNIEnv * env, jclass cls, jlong p, jint channel)
{
	webrtc::VoEEncryption* pVoEEncryption = (webrtc::VoEEncryption*)p;
	jint result = pVoEEncryption->DeRegisterExternalEncryption(channel);
	if(result == 0)
		_Encryption.unset(env);
	return result;
}
